#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>

#define size 256

char* Tranform_alphabet(char *a)
{
	char *ret=a;  //定义一个新的指针来保存a的首地址，防止发生a的首地址被改变
	int i=0;
	while(*(a+i))
	{
		if(*(a+i)>='a'&&*(a+i)<='z')  //判断小写的字符
			*(a+i)-=32;
		if(*(a+i)>='A'&&*(a+i)<='Z')  //判断大写的字符
			*(a+i)+=32;
		i++;
	}
	return ret;
}


int main(void)
{
    int fd[2],fds[2];  //创建两个管道
    char buf[size];
    pid_t pid,pr;
    int len,status;
    if(pipe(fd)<0)  //判断管道1
    {
        perror("failed to pipe");
        exit(1);
    }
    if(pipe(fds)<0)   //判断管道2
    {
        perror("failed to pipe");
        exit(1);
    }
    if((pid=fork())<0)  //判断进程的创建
    {
        perror("failed to fork");
        exit(1);    
    }
    else if(pid>0)  //父进程
    {
        char *str="PARENT PROCESS IS SENDING YOU A MESSAGE\n";
        char strs[50];
        printf("我是父进程啊：；\n");
        close(fd[0]);
        close(fds[1]);
        int data = write(fd[1],str,strlen(str));
        printf("父进程写入%d个字符\n", data);
        read(fds[0],strs,50);
        printf("父进程读出原内容是：%s\n",strs);
        //char * temp = Tranform_alphabet(strs); //不知道为啥没有效果
        char *ret=strs;  
	  int i=0;
	  while(*(strs+i)){
	    if(*(strs+i)>='a'&&*(strs+i)<='z')
		{*(strs+i)-=32;}
	    i++;
	  }
        printf("经过转化父进程读出现内容是：%s\n",ret);
        pr=waitpid(pid,&status,WUNTRACED);
        exit(0);
    }
    else
    {
        char *strs="child process got the message\n";
        printf("我是子进程啊：；\n");
        close(fd[1]);
        close(fds[0]);
        read(fd[0],buf,size);
        printf("子进程读出原内容是：%s\n",buf);
        char * temp = Tranform_alphabet(buf);
        printf("经过转化子进程读出现内容是：%s\n",temp);
        int data = write(fds[1],strs,strlen(strs));
        printf("子进程写入%d个字符\n", data);
        exit(0);
    }
    return 0;
}
